package vista;

import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import modelo.Ejemplo;
import modelo.tabla.ModeloTablaEjemplos;

/**
 *
 * @author Marlon
 */
public class FrmEjemplos extends javax.swing.JDialog
        implements TableModelListener {

    private boolean cambios;
    private List<Ejemplo> listEjemplos;
    private List<String> listNombParamEntradas;
    private ModeloTablaEjemplos modelo;

    @Override
    public void tableChanged(TableModelEvent e) {
        modelo = (ModeloTablaEjemplos) tableEjemplos.getModel();

        System.out.println("LISTA GENERAL");
        mostrarLista(listEjemplos);
        System.out.println("LISTA DE LA TABLA");
        mostrarLista(modelo.getDataEjemplos());

        if (listEjemplos != modelo.getDataEjemplos()) {
            btnGuardar.setEnabled(true);
        }
    }

    class MiRender extends DefaultTableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table,
                Object value,
                boolean isSelected,
                boolean hasFocus,
                int row,
                int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            setOpaque(true);
            setBackground(Color.lightGray);
            setHorizontalAlignment(SwingConstants.CENTER);
            return this;
        }
    }

    /** Creates new form FrmEjemplos */
    public FrmEjemplos(java.awt.Frame parent, boolean modal, List<Ejemplo> listEjemplos,
            List<String> listNombParam) {
        super(parent, modal);
        this.cambios = false;
        String[] a = new String[listNombParam.size()];
        modelo = new ModeloTablaEjemplos(a, new ArrayList<Ejemplo>(listEjemplos));
        
        
        //this.listEjemplos = new ArrayList<Ejemplo>(listEjemplos);
        //this.listEjemplos = listEjemplos;
        this.listNombParamEntradas = new ArrayList<String>(listNombParam);
        
        String[] var = (String[]) listNombParam.toArray(a);
        
        initComponents();
        this.listEjemplos = new ArrayList<Ejemplo>(listEjemplos.size());
        
        for (Ejemplo ejemplo : listEjemplos) {
            this.listEjemplos.add(ejemplo);
        }
        btnGuardar.setEnabled(false);
        initTabla();
        tableEjemplos.getModel().addTableModelListener(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tbarEjemplos = new javax.swing.JToolBar();
        btnAgregar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btnGuardar = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        btnSalir = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableEjemplos = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Lista de Ejemplos");
        setResizable(false);

        tbarEjemplos.setBackground(new java.awt.Color(246, 246, 246));
        tbarEjemplos.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        tbarEjemplos.setFloatable(false);
        tbarEjemplos.setRollover(true);
        tbarEjemplos.setPreferredSize(new java.awt.Dimension(100, 31));

        btnAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Agregar.png"))); // NOI18N
        btnAgregar.setToolTipText("Agregar ejemplo");
        btnAgregar.setFocusable(false);
        btnAgregar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAgregar.setMaximumSize(new java.awt.Dimension(50, 31));
        btnAgregar.setMinimumSize(new java.awt.Dimension(50, 31));
        btnAgregar.setPreferredSize(new java.awt.Dimension(50, 31));
        btnAgregar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });
        tbarEjemplos.add(btnAgregar);

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/eliminar.png"))); // NOI18N
        btnEliminar.setToolTipText("Eliminar ejemplo");
        btnEliminar.setFocusable(false);
        btnEliminar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEliminar.setMaximumSize(new java.awt.Dimension(50, 31));
        btnEliminar.setMinimumSize(new java.awt.Dimension(50, 31));
        btnEliminar.setPreferredSize(new java.awt.Dimension(50, 31));
        btnEliminar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });
        tbarEjemplos.add(btnEliminar);

        jSeparator1.setForeground(new java.awt.Color(246, 246, 246));
        jSeparator1.setEnabled(false);
        jSeparator1.setOpaque(true);
        jSeparator1.setSeparatorSize(new java.awt.Dimension(335, 20));
        tbarEjemplos.add(jSeparator1);

        btnGuardar.setFont(new java.awt.Font("Segoe UI", 1, 11)); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.setFocusable(false);
        btnGuardar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnGuardar.setMargin(new java.awt.Insets(3, 15, 3, 15));
        btnGuardar.setMaximumSize(new java.awt.Dimension(117, 27));
        btnGuardar.setMinimumSize(new java.awt.Dimension(117, 27));
        btnGuardar.setPreferredSize(new java.awt.Dimension(117, 27));
        btnGuardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });
        tbarEjemplos.add(btnGuardar);

        jSeparator2.setEnabled(false);
        jSeparator2.setSeparatorSize(new java.awt.Dimension(10, 25));
        tbarEjemplos.add(jSeparator2);

        btnSalir.setFont(new java.awt.Font("Segoe UI", 1, 11)); // NOI18N
        btnSalir.setText("Salir");
        btnSalir.setFocusable(false);
        btnSalir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSalir.setMargin(new java.awt.Insets(3, 14, 3, 14));
        btnSalir.setMaximumSize(new java.awt.Dimension(117, 27));
        btnSalir.setMinimumSize(new java.awt.Dimension(117, 27));
        btnSalir.setPreferredSize(new java.awt.Dimension(117, 27));
        btnSalir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });
        tbarEjemplos.add(btnSalir);

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

        tableEjemplos.setModel(modelo);
        tableEjemplos.setFillsViewportHeight(true);
        jScrollPane1.setViewportView(tableEjemplos);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tbarEjemplos, javax.swing.GroupLayout.DEFAULT_SIZE, 683, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 673, Short.MAX_VALUE)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(tbarEjemplos, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-699)/2, (screenSize.height-451)/2, 699, 451);
    }// </editor-fold>//GEN-END:initComponents

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        int longEntrada = listEjemplos.get(0).getLongEntrada();
        Ejemplo ejemplo = new Ejemplo(listEjemplos.get(0).getLongEntrada(), listEjemplos.get(0).getLongSalida());
        for (int i = 0; i < longEntrada; i++) {
            ejemplo.addEntrada();
        }
        ejemplo.addSalida();
        ((ModeloTablaEjemplos) tableEjemplos.getModel()).addEjemplo(ejemplo);
    }//GEN-LAST:event_btnAgregarActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        //listEjemplos = modelo.getDataEjemplos();
        btnGuardar.setEnabled(false);
        JOptionPane.showMessageDialog(this, "Se guardo con exito.", "Exito", JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        ((ModeloTablaEjemplos) tableEjemplos.getModel()).deleteContacto(tableEjemplos.getSelectedRow());
    }//GEN-LAST:event_btnEliminarActionPerformed

    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        /*try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmEjemplos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmEjemplos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmEjemplos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmEjemplos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }*/
        //</editor-fold>

    //}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JTable tableEjemplos;
    private javax.swing.JToolBar tbarEjemplos;
    // End of variables declaration//GEN-END:variables

    private void initTabla() {
        tableEjemplos.getTableHeader().setReorderingAllowed(false);
        TableColumn tc = tableEjemplos.getColumnModel().getColumn(0);
        tc.setPreferredWidth(30);
        tc.setResizable(false);
        MiRender tcr = new MiRender();
        tc.setCellRenderer(tcr);
    }

    private void mostrarLista(List<?> listValores) {
        for (Object elemento : listValores) {
            System.out.print(elemento + "  ");
        }
        System.out.println("");
    }
}
